{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 1.1张量\n",
    "## 1.1.1 张量的数据类型"
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "## 导入需要的库\n",
    "import torch\n",
    "## 获取张量的数据类型\n",
    "torch.tensor([1.2,3.4]).dtype\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "execution_count": 1,
   "outputs": [
    {
     "data": {
      "text/plain": "torch.float32"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "torch.float64"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 将张量默认的数据类型设置为其他\n",
    "torch.set_default_tensor_type(torch.DoubleTensor)\n",
    "torch.tensor([1.23,3.4]).dtype"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a.dtype torch.float64\n",
      "a.long()方法： torch.int64\n",
      "a.int()方法： torch.int32\n",
      "a.float()方法： torch.float32\n"
     ]
    }
   ],
   "source": [
    "## 将张量数据转换为整型\n",
    "a = torch.tensor([1.2,5.6])\n",
    "print(\"a.dtype\",a.dtype)\n",
    "print(\"a.long()方法：\", a.long().dtype)\n",
    "print(\"a.int()方法：\", a.int().dtype)\n",
    "print(\"a.float()方法：\", a.float().dtype)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "torch.float32"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 恢复torch默认的数据类型\n",
    "torch.set_default_tensor_type(torch.FloatTensor)\n",
    "torch.tensor([1.5,2.9]).dtype"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "torch.float32"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 获取默认的数据类型\n",
    "torch.get_default_dtype()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1.1.2 张量的生成\n",
    "（1）使用torch.tensor()函数构造张量"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([2, 2])\n",
      "torch.Size([2, 2])\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "A = torch.tensor([[1.2,2.2],[2,3]])\n",
    "#获取张量的维度\n",
    "print(A.shape)\n",
    "#获取张量的形状大小\n",
    "print(A.size())\n",
    "#计算张量中包含元素的数量\n",
    "print(A.numel())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}